/**
 * 
 */
package com.huangliusog.security;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.huangliusong.entity.User;

/**
 * @author huangliusong E-mail:huangliusong1994@gmail.com
 * @version 创建时间：2018年6月13日 下午5:07:30 类说明
 */
public class SecurityManagerSupport implements UserDetailsService {
	private static Logger logger = LoggerFactory
			.getLogger(SecurityManagerSupport.class);

	@Override
	public UserDetails loadUserByUsername(String userName) {
		try {
			User userFromQuery = new User(new Long(123), "admin", "admin", true);
			logger.debug("查询得到用户：{}", userFromQuery);
			                 
			List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
			GrantedAuthority au = new SimpleGrantedAuthority("ROLE_USER");
			list.add(au);
			logger.debug("得到其权限：{}", list);
			return new User(new Long(12), "admin", "admin", true, list);
		} catch (EmptyResultDataAccessException e) {
			logger.debug("查询结果集为空:{}", userName);
			throw new UsernameNotFoundException("用户名或密码不正确");
		}
	}

}
